extends layout

block content
  p 登录用户为： #{username}
  p locals: #{env}

  .rss-subscribe
    a.btn-rss(href='/api/rss' target='_blank' title='订阅 RSS')
      span.rss-icon 📡
      span.rss-text 订阅 RSS
    .rss-formats
      a.rss-format-link(href='/api/rss' target='_blank' title='RSS 2.0') RSS
      span.separator |
      a.rss-format-link(href='/api/rss/atom' target='_blank' title='Atom 1.0') Atom
      span.separator |
      a.rss-format-link(href='/api/rss/json' target='_blank' title='JSON Feed') JSON

  .mail-section
    button#sendTestMail.btn-mail
      span.mail-icon 📧
      span.mail-text 发送测试邮件
    .mail-info
      span.mail-recipient 收件人: 992620216@qq.com
      span.separator |
      span.mail-subject 主题: 测试博客
    .mail-result#mailResult(style='display: none;')

  script.
    document.getElementById('sendTestMail').addEventListener('click', async function() {
      const resultDiv = document.getElementById('mailResult');
      const btn = this;

      try {
        btn.textContent = '发送中...';
        btn.disabled = true;

        const response = await fetch('/api/mail/send', {
          method: 'POST',
          headers: {
            'Content-Type': 'application/json',
          }
        });

        const result = await response.json();

        if (result.success) {
          resultDiv.innerHTML = `
            <div class="success-message">
              <p>✅ ${result.message}</p>
            </div>
          `;
        } else {
          resultDiv.innerHTML = `
            <div class="error-message">
              <p>❌ ${result.message}</p>
            </div>
          `;
        }

        resultDiv.style.display = 'block';

      } catch (error) {
        resultDiv.innerHTML = `
          <div class="error-message">
            <p>❌ 发送失败: ${error.message}</p>
          </div>
        `;
        resultDiv.style.display = 'block';
      } finally {
        btn.textContent = '发送测试邮件';
        btn.disabled = false;
      }
    });

  hr
  h1.head-title 文章列表

  if posts
    each post, i in posts
      .post
        h4
          a(href='/posts/show/#{post._id}')
            =post.title
        p.meta Posted in
          span.tag
            a(href='/categories/show/'+ post.categoryId) #{ post.category_title }
          span &nbsp; on #{dayjs(post.date).format("MM-DD-YYYY")}
        img.thumb(src=post.thumbimage)
        .body
          if post.content.length > 200
            !=truncateText(post.content, 200)
          else
            !=post.content
        a.more(href='/posts/show/' + post.id) Read More
  else
    p.no-post No Posts to Display Yet! Start Writing!
